package com.hc.sensitive.factory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据库中获取敏感词列表
 */
@Component
public class TxtWordFactory implements IWordFactory {
    private static final Logger logger = LoggerFactory.getLogger(TxtWordFactory.class);
    @Override
    public List<String> getWordList() {
        List<String> wordList = new ArrayList<>();
        try (
                InputStream is = this.getClass().getClassLoader().getResourceAsStream("sensitive-words.txt");
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        ) {
            String keyword;
            while ((keyword = reader.readLine()) != null) {
                // 添加到前缀树
                wordList.add(keyword);
            }
        } catch (IOException e) {
            logger.error("加载敏感词文件失败: " + e.getMessage());
        }
        return wordList;
    }
}
